$(function () {

    // 分类筛选
    sortSelFn();

    listCourse(1);

    $('body').on('click', '#filter-ensure', function () { // 确定筛选
        $('#page-infinite-scroll-bottom').unbind();
        resetScrollFn();
        listCourse(1);
        closeFilterPanel();
    }).on('click','.course-info',function () {
        window.location.href = baseloaction + '/mobile/course/info?courseId='+ $(this).data('id');
    }).on('click', '#filter-clean', function () { // 清空筛选
        cleanFilter();
    })/*.on('click', '#filter-subject-son li', function () { // 科目筛选
        if ($(this).hasClass('active') ) return;
        activeFilterCondition(this);
        // setSubjectIds();
        listCourse(1);
        closeFilterPanel();
    })*/.on('click', '#filter-order li', function () { // 排序
        $("#filter-order li").removeClass("active");
        $(this).addClass("active");
        var order = $(this).data('order');
        if(order==1){
            $(this).data("order",2);
        }else if(order==2){
            $(this).data("order",1);
        }else if(order==3){
            $(this).data("order",4);
        }else if(order==4){
            $(this).data("order",3);
        }
        $('#orderType').val(order);
        closeFilterPanel();
        resetScrollFn();
        listCourse(1);
    }).on('click', '#filter-type li', function () { // 类型筛选
        if ($(this).hasClass('active')) return;
        activeFilterCondition(this);
        $('#type').val($(this).data('type'));
    }).on('click', '#filter-year li', function () { //年份筛选
        if ($(this).hasClass('active')) return;
        activeFilterCondition(this);
        $('#year').val($(this).data('year'));
    }).on('keypress','#search',function(e){
        var keycode = e.keyCode;
        if(keycode=='13') {
            e.preventDefault();
            resetScrollFn();
            listCourse(1);
        }
    }).on('click','#searchBut',function () {//搜索
        resetScrollFn();
        listCourse(1);
    }).on('click','.searchbar-cancel',function () {//取消搜索
        $("#search").val('');
        resetScrollFn();
        listCourse(1);
    }).on('click','#subject,#filter',function () {
        closeFilterPanel();
    });

    function resetScrollFn() {
        Zepto('#course-list').empty();
        Zepto("#page-infinite-scroll-bottom").find(".infinite-scroll").scrollTop(0);
    }

    // 专业 课程类型 排序筛选器
    function activeFilterCondition(obj) {
        $(obj).addClass('active').siblings().removeClass('active');
    }

    // 清空筛选
    function cleanFilter() {
        $('#filter-type, #filter-year').each(function () {
            $(this).children().eq(0).addClass('active').siblings().removeClass('active');
            $("#type").val("");
            $("#year").val("");
        });
    }

    // 关闭筛选面板
    function closeFilterPanel() {
        $('.wm-cl__wrap').removeClass('sortSeOpen')
    }

    // 年筛选器
    function initYearFilter() {
        var now = new Date().getYear() + 1900;
        for (var year = 2016; year <= now + 2; year++) {
            $('#filter-year').append('<li class="col-25" data-year="' + year + '">' +
                '<a href="javascript:void(0)" class="button button-round">' + year + '</a>' +
                '</li>');
        }
    }

    // 课程加载
    function listCourse(pageNo) {
        var params = $('#search-course-form').serializeJSON();
        params = JSON.stringify(params);
        loadMobilePathData({
            url: baseloaction + '/mobile/course/ajaxCourseList',
            contextId: 'course-list',
            pageNo: pageNo,
            pageCall: listCourse,
            params: params,
            callback: function (pageNo) {
            }
        });
    }

    // 分类筛选
    function sortSelFn() {
        initYearFilter();
        var bEle = $('.wm-cl__wrap');
        $('.wm-buttons-select-tab__wrap').on('click', '.tab-link', function () {
            if($(this).hasClass('active')){
                if(bEle.hasClass('sortSeOpen')){
                    bEle.removeClass('sortSeOpen');
                }else {
                    bEle.addClass('sortSeOpen');
                }
            }else {
                activeFilterCondition(this);
                bEle.addClass('sortSeOpen');
            }
        });
        $('.modal-overlay').on('click', function () {
            if (bEle.hasClass('sortSeOpen')){
                bEle.removeClass('sortSeOpen');
            }
        });
        //初始化专业
        initSubjectData();
    }

    /**
     * 初始化专业数据
     */
    function initSubjectData() {
        $('#filter-subject-parent').html("");
        /**
         * 初始化专业
         */
        findSubjectList(function (result) {
            var allSubject = result;
            var parentSubjectList = allSubject[0];
            $('#filter-subject-parent').append('<li data-subject="0">全部专业</li>');
            $.each(parentSubjectList,function (index,parentSubject) {
                $('#filter-subject-parent').append('<li data-subject="' + parentSubject.id + '">' + parentSubject.subjectName + '</li>');
            });
            var list = $("#filter-subject-parent li");
            if(list!=null && list.length>0){
                var currentSubjectId = $('#subjectIds').val();
                var subjectId ;
                if(currentSubjectId != 0){
                    $.each(list,function (index,parent) {
                        subjectId = $(parent).data('subject');
                        if(currentSubjectId == subjectId){
                            $(parent).addClass('active');
                            return false;
                        }
                    });
                }else {
                    $(list[0]).addClass('active');
                    subjectId = $(list[0]).data("subject");
                }
                initChildSubject(subjectId);
            }
            $("#filter-subject-parent li").click(function () {
                if ($(this).hasClass('active')) return;
                $("#filter-subject-parent li").removeClass("active");
                $(this).addClass("active");
                var subjectId = $(this).data('subject');
                $("#subjectIds").val(subjectId);
                initChildSubject(subjectId);
            });

            /**
             * 初始化子级科目
             * @param subjectId 父级专业ID
             */
            function initChildSubject(subjectId) {
                //$("#search-course-form>#subjectIds").val(subjectId);
                var childList = allSubject[subjectId];
                if(childList==null || childList.length<=0){
                    $("#no-data-tips").removeClass("hide");
                    $("#filter-subject-son").addClass("hide");
                }else{
                    $("#no-data-tips").addClass("hide");
                    $("#filter-subject-son").removeClass("hide");
                    $("#filter-subject-son").html('<li class="col-auto" id="filter" data-subject=""><a class="button button-round" href="javascript:void(0);">全部</a></li>');
                    if(subjectId == 0){
                        $("#filter-subject-son").html('<li class="col-auto">' +
                            '<a href="/mobile/course/courseList" class="button button-round">全部</a>' +
                            '</li>');
                    }else{
                        $.each(childList,function (index,subject) {
                            $("#filter-subject-son").append('<li class="col-auto"  data-parent="' + subject.parentId + '" data-subject="' + subject.id + '">' +
                                '<a href="javascript:void(0)" id="subject" class="button button-round">' + subject.subjectName + '</a>' +
                                '</li>');
                        });
                    }
                    //点击科目时
                    $("#filter-subject-son li").click(function () {
                        if ($(this).hasClass('active')){
                            closeFilterPanel();
                            return;
                        }
                        $(this).addClass('active').siblings().removeClass('active');
                        var id = $(this).data("subject");
                        if(id){
                            $("#subjectIds").val(id);
                        }else{
                            var parentId = $(this).next('li[data-parent]').data('parent');
                            $('#subjectIds').val(parentId);
                        }
                        $('#page-infinite-scroll-bottom').unbind();
                        closeFilterPanel();
                        listCourse(1);
                    });
                }
            }
        });
    }
});